package example.springboot.controller;

import example.springboot.common.Result;
import example.springboot.dto.ChartDataDTO;
import example.springboot.dto.StatisticsDTO;
import example.springboot.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
@RestController
@RequestMapping("/api/admin/statistics")
@CrossOrigin
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    /**
     * 获取统计数据
     */
    @GetMapping
    public Result<StatisticsDTO> getStatistics() {
        try {
            StatisticsDTO statistics = statisticsService.getStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取统计数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取用户增长趋势
     */
    @GetMapping("/user-growth")
    public Result<List<ChartDataDTO>> getUserGrowthTrend(
            @RequestParam(defaultValue = "30") int days
    ) {
        try {
            List<ChartDataDTO> data = statisticsService.getUserGrowthTrend(days);
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取用户增长趋势失败：" + e.getMessage());
        }
    }

    /**
     * 获取学习活跃度趋势
     */
    @GetMapping("/study-activity")
    public Result<List<ChartDataDTO>> getStudyActivityTrend(
            @RequestParam(defaultValue = "7") int days
    ) {
        try {
            List<ChartDataDTO> data = statisticsService.getStudyActivityTrend(days);
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取学习活跃度趋势失败：" + e.getMessage());
        }
    }
}

